/**
 * 模拟模式对话框
 */
var yaModal=new Class({
    Implements:Options,
    options:{
        size:[400,300],
        opacity:0.5,
        zIndex:1000,
        modal:true,
        dragable:true,
        resizeable:false
    },
    isShow:false,
    draging:false,
    initialize:function(appendTo){
        var apt=$(appendTo);
        var defCss={
            position:'absolute',
            top:'0pt',left:'0pt',
            display:'none'
        };
        //遮罩层
        this.boxMask=new Element('div',{
            'styles':defCss
        });
        apt.grab(this.boxMask);
        //主层
        this.boxContainer=new Element('div',{
            'styles':defCss
        });
        apt.grab(this.boxContainer);
        //标题栏
        this.boxTitle=new Element('div',{
            'styles':{
                'cursor':'move',
                'height':'30px',
                'background-color':'#03629d'
            }
        });
        this.boxContainer.grab(this.boxTitle);
        //标题栏-关闭区域
        this.boxTitleClose=new Element('div',{
            'styles':{
                'float':'right',
                'width':'12px',
                'height':'30px',
                'padding':'0 9px',
                'cursor':'default'
            }
        });
        this.boxTitle.grab(this.boxTitleClose);
        //标题栏关闭按钮
        this.boxTitleCloseImg=new Element('div',{
            'styles':{
                'width':'13px',
                'height':'13px',
                'margin-top':'8px',
                'cursor':'pointer',
                'background-color':'#ff171a'
            },
            'title':'关闭',
            'html':'<img src="'+_TMPL_+'../Admin/Public/img/close.gif'+'"/>'
        });
        this.boxTitleCloseImg.addEvents({
            'click':function(){
                this.close();
            }.bind(this),
            'mouseover':function(){
                this.setStyle('background-color','#d10003');
            },
            'mouseout':function(){
                this.setStyle('background-color','#ff171a');
            }
        });
        this.boxTitleClose.grab(this.boxTitleCloseImg);
        //标题栏--标题部分
        this.boxTitleLabel=new Element('div',{
            'styles':{
                'text-indent':'12px',
                'font-weight':'bold',
                'line-height':'30px',
                'color':'#fff'
            }
        });
        this.boxTitle.grab(this.boxTitleLabel);
        //主内容
        this.boxMain=new Element('div');
        this.boxContainer.grab(this.boxMain);
        //拖拽效果
        if(this.options.dragable){
            new Drag.Move(this.boxContainer,{
                handle:this.boxTitleLabel,
                onBeforeStart:function(){
                   this.draging=true;
                }.bind(this)
            });
        }
        //大小改变效果
        if(this.options.resizeable){
            var resizeInst=this.boxContainer.makeResizable();
            resizeInst.addEvent('snap',function(){
                if(this.draging) resizeInst.stop();
                this.draging=false;
            }.bind(this));
        }
        //绑定大小调节函数
        $(window).addEvent('resize',function(){
            this.resize();
        }.bind(this));
    },
    pack:function(){
        var winpos=$(window).getSize();
        //对遮罩层进行设置
        if(this.options.modal){
            this.boxMask.setStyles({
                'width':winpos.x+'px',
                'height':winpos.y+'px',
                'opacity':this.options.opacity,
                'z-index':this.options.zIndex,
                'background-color':'#039af5',
                'cursor':'wait',
                'display':'block'
            });
        }else{
             this.boxMask.setStyle('display','none');
        }
        //对主层进行设置
        var left=(winpos.x-this.options.size[0])/2;
        var top=(winpos.y-this.options.size[1])/2;
        this.boxContainer.setStyles({
            'left':(left>20)?left:20+'px',
            'top':(top>20)?top:20+'px',
            'width':this.options.size[0]+'px',
            'height':this.options.size[1]+'px',
            'border':'2px solid #03629d',
            'background-color':'#fff',
            'z-index':this.options.zIndex+2,
            'cursor':'default',
            '-moz-border-radius':'4px',
            '-webkit-border-radius':'4px',
            'display':'block'
        });
        //对标题进行设置
        if( ! 'title' in this.options ) this.options.title='Windows Dialog';
        this.boxTitleLabel.set('html',this.options.title);
        //对主内容进行设置
        this.boxMain.setStyles({
            'height':(this.options.size[1]-30)+'px',
            'display':'block'
        });
        this.isShow=true;
    },
    resize:function(){
        if(this.isShow){
            var winpos=$(window).getCoordinates();
            this.boxMask.setStyles({
                'width':winpos.width+'px',
                'height':winpos.height+'px'
            });
            //对主层进行设置
            var left=(winpos.width-this.options.size[0])/2;
            var top=(winpos.height-this.options.size[1])/2;
            this.boxContainer.setStyles({
                'left':(left>20)?left:20+'px',
                'top':(top>20)?top:20+'px'
            });
        }
    },
    close:function(){
        this.boxMask.setStyle('display','none');
        this.boxContainer.setStyle('display','none');
    },
    html:function(options){
        this.setOptions(options);
        var iframe=new Element('iframe',{
            'class':'modalIframe',
            'frameborder':'0',
            'scrolling':'no',
            'src':this.options.src,
            'width':'100%',
            'height':'100%'
        });
        this.boxMain.empty();
        this.boxMain.grab(iframe);
        this.pack();
    },
    fragment:function(frag){
        this.boxMain.empty();
        this.boxMain.grab(frag);
        this.pack();
    }
});
